﻿package model.drawPanel;
import java.util.ArrayList;

import model.drawPanel.Interface.IDrawObserver;
import model.drawPanel.Interface.IDrawPanelModel;
import model.drawPanel.entitys.Circle;
import model.drawPanel.entitys.Entity;
import model.drawPanel.entitys.ITextChanger;
import model.drawPanel.entitys.Line;
import model.drawPanel.entitys.Pen;
import model.drawPanel.entitys.Picture;
import model.drawPanel.entitys.Rectangle;
import model.drawPanel.entitys.Text;

import java.io.File;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.KeyListener;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.events.MouseMoveListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.ImageLoader;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Synchronizer;
import org.eclipse.ui.internal.Model;
import org.eclipse.wb.swt.SWTResourceManager;

import controller.drawPanel.Interface.IDrawPanelController;
/*
 * 管理绘图的指令集合，绘图元素，有更新则update（）绘图界面重画
 * 
 * 封装绘图指令，转发给别人，,通过“隔断信息”标识一次指令的结束oneOrderIsDone（），服务器负责第一次进入的用户的初始化
 * 
 * 第一种方案 ，主席用udp传绘图指令，服务器转发单条指令，由各自 “终端用户”保存指令队列处理redo undo ，
 * 优点： 快，传输量小，不稳定，容易丢包
 * 第二种方案 ， 假如udp 丢包严重，采取 主席tcp传给服务器，服务器udp发送整个绘图列表（有重复部分），图片文件则用tcp传一次给非主席人员（不重复发），
 *  “服务器” 保存指令列表处理redo undo
 *  优点： 稳定，保证不会有不同步的地方，但比较慢，传输量大
 * 
 * 所有荧光棒都采取udp的方式
 * 
 * order.drawType 1 表示是智能模型 0表示pen和橡皮 ，， 绘画时是先画pen和eraser的相关信息，因为只有pen可以擦，其他智能模型不可擦，所以分2个列表保存
 * panel的增删改 ，权限申请，撤销，恢复，保存均用String传信息
 */
public class DrawPanelModel implements IDrawPanelModel ,IReceiver,MouseMoveListener , MouseListener ,KeyListener{
	private int roomID;
	private IDrawNetModel netModel;
	private IDrawObserver ui;
	//---------------------------------------
	private  ArrayList<Entity> entityList;
	private ArrayList<Entity>penList;
	private ArrayList<Panel>panelList;
	private ArrayList<DrawOrder>orderList;
	private ArrayList<DrawOrder>redoList;
	private DrawOrder temp_order;//=null 标识本次为新的指令开始
	//--------------------进入房间与房间已有内容同步所用------------------------------
	//--------------------------------------------------------------
	private Entity newEntity;//心添加的并不断再更改的元素
    private int x1  = -1, y1  = -1;//记录每次的第一个点，与后来不断变化的第二个点形成图元对象
    private int x2=-1,y2=-1;
    private boolean authority = false;//是否有操作权
    private int  tool = 1,penSize,eraserSize;
    private Entity choosedEntity ,focusedEntity ;
    private String zoomMsg;//缩放的类型信息，上下左右 左上左下右上右下
    private int moveStatus = 0;// 表示鼠标移动时是什么状态 0空闲 1进入元素范围内,2进入选中元素缩放圈范围内，3画，4移动，5缩放
    private int mouseStatus = 0;//0 是箭头，处理移动缩放，1是画笔，处理绘画
    private boolean mouseIsPressed = false;
    private int entityIDCounter = 0;
    private boolean textIsChoosed = false;//标记选中的是不是text
    /* tool 
     * 1 = pen 
     * 2 = line
     * 3 = circle
     * 4 = rectangle
     * 5 = pictrue
     * 6 = text
     * 7 = fillcolor
     * 8 = eraser
     * 9 = 
     */	 	 
    private Color color = new Color(Display.getCurrent(),0,255,254);
    private int width = 20;//橡皮，画笔，还有各种线条的宽度
    private int style = SWT.LINE_CUSTOM;//线条的风格，dot dash 实线	
    private boolean hasFill = false;
    private static StyledText textInput;
	//--------------------------------------------------
	public DrawPanelModel (IDrawObserver drawObserver,int roomID){
		ui = drawObserver;
		netModel = new DrawNetModel(roomID);
		netModel.addReceiver(this);
		panelList = new ArrayList<Panel>();
		Panel panel = new Panel();
		panelList.add(panel);
		entityList = panel.getEntitList();
		penList = panel.getPenList();
		orderList = panel.getOrderList();
		redoList = panel.getRedoList();
		temp_order = null;
		this.roomID = roomID;
	}
	@Override
	public void setTextInput(StyledText text) {
		this.textInput = text;
		
	}
	public static StyledText getStyledText(){
		return textInput;
	}
	 public void setCursor(int mouseStatus){
		 //0代表箭头，1代表画笔
		 this.mouseStatus = mouseStatus;
		 moveStatus = 0;
		 btnIsPressed();
	 }
	private  void  oneOrderIsDone() {
		if(temp_order==null){
			System.out.println("null is added into orderList----------------------------------------------------------" +
					"---------------------------------------------------------------------------------------------------");
		}
		orderList.add(temp_order);
		netModel.sendOrder(new DrawOrder(-1));
		System.out.println("one order is done");
		temp_order = null;
	}
	private void btnIsPressed(){
		if(choosedEntity !=null){
			
			if(ITextChanger.class.isAssignableFrom(choosedEntity.getClass())){
				
				ITextChanger t = (ITextChanger) choosedEntity;
				 if(t.getFocusedString()==null||!t.getFocusedString().equals(textInput.getText())){
					System.out.println("textchange-----------");
					temp_order = new DrawOrder_textChange(choosedEntity.getId(),t.getFocusedRowNum(),t.getFocusedString(),textInput.getText());
					netModel.sendOrder(temp_order);
					oneOrderIsDone();
				}
			}
			choosedEntity.cancelChoosed();
			choosedEntity = null;
			ui.refresh();
		}
		 
	}
	private void addEntity(Entity entity,int newx2, int newy2) {
		if(temp_order ==null){//第一次
			temp_order= new DrawOrder_ADD(entity);
			String type = entity.getClass().getName();
			if(type.equals("model.drawPanel.entitys.Pen")){
				temp_order.setDrawType(0);
			}
			if(temp_order.getDrawType()==0){
				temp_order.execute(penList);
			}else{
			temp_order.execute(entityList);
			}
			netModel.sendOrder(temp_order);
			
		}else{
			temp_order.change(newx2, newy2);
			if(temp_order.getDrawType()==0){
				temp_order.execute(penList);
			}else{
			temp_order.execute(entityList);
			}
			netModel.sendOrder(new DrawOrder_Change(newx2,newy2));
		}
		ui.refresh();
		
	}
	private void deleteEntity(Entity en) {
			temp_order = new DrawOrder_DELETE(en);
			entityList.remove(en);
			choosedEntity.cancelChoosed();
			choosedEntity =null;
			ui.refresh();
			netModel.sendOrder(temp_order);
			System.out.println("delete is done");
			oneOrderIsDone();
			return;
		
	}
	public Entity getByID(int id){
		for(Entity en:entityList){
			if(en.getId() ==id){
				return en;
			}
		}
		return null;
	}
	private void changePosition(Entity en, int x, int y) {//x y 为移动的距离矢量  就是有正负
		if(temp_order == null){//第一次移动
			temp_order = new DrawOrder_Move(en.getId(), x ,y);
			netModel.sendOrder(temp_order);
		}else{
		temp_order.change(x, y);
		netModel.sendOrder(new DrawOrder_Change(x,y));
		}
		temp_order.execute(entityList);
		ui.refresh();
	}	
	private void changeSize(Entity en,String type ,int xmove,int ymove ){	
		if(temp_order == null){//第一次缩放
			temp_order = new DrawOrder_Size(en.getId() , type ,  xmove ,ymove);
			netModel.sendOrder(temp_order);
		}else{
		temp_order.change(xmove, ymove);
		netModel.sendOrder(new DrawOrder_Change(xmove,ymove));
		}
		temp_order.execute(entityList);
		ui.refresh();
	}
	@Override
	public void undo() {
		// TODO Auto-generated method stub
		 btnIsPressed();
		 int index =orderList.size()-1;
		 if(index <0 || !authority){
			 return;
		 }
		 DrawOrder order =orderList.get(index);
		 if(order.getDrawType()==1){
		 order.undo(entityList);
		 }else{
			 order.undo(penList);
		 }
		 ui.refresh();
		 redoList.add(order);
		 orderList.remove(index);
		 netModel.sendOrder(new OperationOrder(roomID, OperationOrder.UNDO));
		
	}

	@Override
	public void redo() {
		// TODO Auto-generated method stub
		 btnIsPressed();
		 int index = redoList.size()-1;
		 if(index<0 || !authority){
			 return;
		 }
		 DrawOrder order =redoList.get(index);
		 if(order.getDrawType()==1){
		 order.execute(entityList);
		 }else{
			 order.execute(penList);
		 }
		 orderList.add(order);
		 redoList.remove(index);
		 ui.refresh();
		 netModel.sendOrder(new OperationOrder(roomID, OperationOrder.REDO));
		
	}


	@Override
	public void addPanel() {
		 btnIsPressed();
		 panelList.add(new Panel());
		 netModel.sendOrder(new OperationOrder(roomID,OperationOrder.ADD_PANEL));
		
	}

	@Override
	public void deletePanel(int id) {
		 btnIsPressed();
		 panelList.remove(id);
		 netModel.sendOrder(new OperationOrder(roomID,OperationOrder.DELETE_PANEL + id));
		 if(id==0){
			 Panel p = new Panel();
			 entityList = p.getEntitList();
			 penList = p.getPenList();
			 orderList =p.getOrderList();
			 redoList = p.getRedoList();
			 entityIDCounter =0;
			 panelList.add(p);
			 ui.refresh();
			 
		}
	}

	@Override
	public void choosePanle(int id) {
		 btnIsPressed();
		entityList = panelList.get(id).getEntitList();
		penList = panelList.get(id).getPenList();
		orderList =panelList.get(id).getOrderList();
		redoList = panelList.get(id).getRedoList();
		ui.refresh();
		 netModel.sendOrder(new OperationOrder(roomID,OperationOrder.CHOOSE_PANEL + id));
	}

	@Override
	public void save(String fileName) {
		if(fileName.equals("\\")){
			return;
		}
		 btnIsPressed();
		// netModel.sendOrder(new OperationOrder(roomID,OperationOrder.SAVE + fileName));
		Image save = new Image(Display.getDefault(),930,580);
		GC gc = new GC(save);
		draw(Display.getDefault(),gc, 930,580);
		ImageLoader loader = new ImageLoader();
		loader.data = new ImageData[]{save.getImageData()};
		loader.save(fileName, SWT.IMAGE_JPEG);
	}
	public synchronized void draw(Display display , GC gc,int width,int height){
//	     Image image = new Image(display,drawCanvas.getSize().x,drawCanvas.getSize().y);
//	     GC imageGC = new GC(image);
//	     imageGC.setBackground(SWTResourceManager.getColor(0, 0, 255));
//	     imageGC.setForeground(gc.getForeground());
//	     org.eclipse.swt.graphics.Rectangle imageSize = image.getBounds();   
//	     imageGC.fillRectangle(0, 0, imageSize.width + 1, imageSize.height + 1); //填充背景  
		for(Entity en :entityList){
			if(!authority){
				en.cancelChoosed();
			}
			en.draw(display, gc);
	     }
//		for(Entity en:penList){
//			en.draw(display, gc);
//		}
		
		if(penList.size() >0){
			Image image = new Image(display,width,height);
			
			GC imageGC = new GC(image); 
			for(Entity en:penList){
				en.draw(display, imageGC);
			}
			ImageData srcData =image.getImageData();
			int whitePixel =srcData.palette.getPixel(new RGB(255,255,255));
			srcData.transparentPixel =whitePixel;
			image.dispose();
			image = new Image(display,srcData);
			gc.drawImage(image, 0, 0);
			image.dispose();
			imageGC.dispose();
		}
		if(choosedEntity !=null){
			choosedEntity.draw(display, gc);
		}
		
	}

	public synchronized void receive(Object object) {
		if(object.getClass().getName().equals("model.drawPanel.IniteOrder")){
			inite((IniteOrder)object);
			ui.refresh();
			return;
		}
		//operation
		if(((Order)object).getType()==1){
				//OperationOrder
				if(object.getClass().getName().equals("model.drawPanel.OperationOrder")){
				OperationOrder order = (OperationOrder)object;
				String msg = order.getMsg();
				if(msg.startsWith(OperationOrder.ADD_PANEL)){
					System.out.println("add panel");
					 panelList.add(new Panel());
					 ui.addPanel();
					 ui.refresh();
					 return;
				}
				if(msg.startsWith(OperationOrder.DELETE_PANEL)){
					System.out.println("delete panel");
					int index =Integer.valueOf(msg.substring(11));
					 panelList.remove(index);
					 if(index==0){
						 Panel p = new Panel();
						 entityList = p.getEntitList();
						 penList = p.getPenList();
						 orderList =p.getOrderList();
						 redoList = p.getRedoList();
						 entityIDCounter =0;
						 panelList.add(p);
					}
					 ui.deletePanel();
					 ui.refresh();
					 return;
				}
				if(msg.startsWith(OperationOrder.CHOOSE_PANEL)){
					System.out.println("choose panel");
					 int id = Integer.valueOf(msg.substring(11));
						entityList = panelList.get(id).getEntitList();
						penList = panelList.get(id).getPenList();
						orderList =panelList.get(id).getOrderList();
						redoList = panelList.get(id).getRedoList();
						ui.choosePanel(id);
						ui.refresh();
						return;
				}
				if(msg.startsWith(OperationOrder.REDO)){
					 int index = redoList.size()-1;
					 System.out.println("redo-----------"+index);
					 if(index<0){
						 return;
					 }
					 DrawOrder temp =redoList.get(index);
					 if(temp.getDrawType()==1){
					 temp.execute(entityList);
					 }else{
						 temp.execute(penList);
					 }
					 orderList.add(temp);
					 redoList.remove(index);
					 ui.refresh();
					 return;
				}if(msg.startsWith(OperationOrder.UNDO)){
					 int index =orderList.size()-1;
					 if(index <0){
						 return;
					 }
					 DrawOrder temp =orderList.get(index);
					 if(temp.getDrawType()==1){
					 temp.undo(entityList);
					 }else{
						 temp.undo(penList);
					 }
					 ui.refresh();
					 redoList.add(temp);
					 orderList.remove(index);
					 return;
				}
				if(msg.startsWith(OperationOrder.AUTHORITY_PASS)){
					try {
						if(order.getMsg().substring(9).equals( InetAddress.getLocalHost().getHostAddress().toString()) ){
							authority = true;
						}else{
							authority = false;
						}
						ui.updateAuthoriy(order.getMsg().substring(9));
					} catch (UnknownHostException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}	
			}
		}
		//draw
		else{
			//change 信息
			 if(object.getClass().getName().equals("model.drawPanel.DrawOrder_Change")){
				DrawOrder_Change change = (DrawOrder_Change)object;
				temp_order.change(change.x	, change.y);
			}
		 //drawOrder
			else{
				DrawOrder order = (DrawOrder)object;
		//drawOrder分隔符
				if(order.getId() == -1){
					orderList.add(temp_order);
					temp_order =null;
					return;
				}
		//新的drawOrder	
				else{
					temp_order = order;
					if(order.getClass().getName().equals("model.drawPanel.DrawOrder_ADD")){
						entityIDCounter++;
					}
				}	
			}
			if(temp_order.getDrawType()==0){
				temp_order.execute(penList);
			}else{
			temp_order.execute(entityList);
			}
			ui.refresh();
		}
		
}

	@Override
	public void requestAuthority() {
		 btnIsPressed();
		try {
			netModel.sendOrder(new OperationOrder(roomID ,OperationOrder.AUTHORITY_REQUEST+InetAddress.getLocalHost().getHostAddress()));
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	public void passAuthority(String id) {
		 btnIsPressed();
		netModel.sendOrder(new OperationOrder(roomID,OperationOrder.AUTHORITY_PASS + id));
	}
	@Override
	public void mouseDoubleClick(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void mouseDown(MouseEvent e) {
		if(!authority){
			return;
		}
		x1 = e.x;
		y1 = e.y;
		x2 =e.x;
		y2 =e.y;
		mouseIsPressed = true;
		
		if(mouseStatus ==0){
			//鼠标是箭头,只处理选中移动缩放
			if(moveStatus == 1){//进入元素内部按下鼠标拖动表示选中
				if(choosedEntity ==null&& focusedEntity != null){
					choosedEntity = focusedEntity;
					choosedEntity.setChoosed();
					ui.refresh();
					return;
				}
				else if (choosedEntity !=null &&focusedEntity ==null){
					choosedEntity.cancelChoosed();
					choosedEntity = null;
					ui.refresh();
					return;
				}
				else if(choosedEntity.getId()!= focusedEntity.getId()){
					choosedEntity.cancelChoosed();
					choosedEntity =focusedEntity;
					choosedEntity.setChoosed();
					
					ui.refresh();
					return;
				}
				//choosedEntity = focusedEntity
				return;
			}else if(moveStatus ==0){
				//空闲状态点击，取消原来的选中
				if(choosedEntity != null){
					choosedEntity.cancelChoosed();
					ui.refresh();
					choosedEntity = null;
				}
			}
		}else if(mouseStatus == 1){
			//鼠标是画笔
			moveStatus = 3;//变成画笔状态
		}
		
		
		
	}
	@Override
	public void mouseUp(MouseEvent e) {
		if(!authority){
			return;
		}
		mouseIsPressed = false;
		newEntity = null;
		if((x1 !=x2 || y1!=y2) && moveStatus!= 0){
			System.out.println("here one order is done" +x1+" "+y1+" "+x2+" "+y2);
			oneOrderIsDone();//相等表示鼠标只是点了一下并没用move，不发送指令
		
		}else if(x1==x2 && y1==y2){
			if(focusedEntity == null){
				btnIsPressed();
			}
		}
		x1 = -1;
		y1 = -1;
		y2=-1;
		x2=-1;
		
		
	}
	@Override
	public void mouseMove(MouseEvent e) {
		//1鼠标按下
		if(!authority){
			return;
		}
			
		if(mouseIsPressed){
			if(mouseStatus==1){//空闲状态，绘画鼠标，按下鼠标标示绘画了
				switch(tool){
				case 1://pen
					if(newEntity ==null){
						newEntity =new Pen(entityIDCounter++,color,width,x1,y1 ,e.x,e.y);
					}
					addEntity(newEntity, e.x, e.y);
					break;
				case 2://line
					if(newEntity == null){
					newEntity = new Line(entityIDCounter++,color,width,style,
							  x1 , y1 , e.x , e.y );}
					addEntity(newEntity , e.x , e.y);

					break;
				case 3://circle
					if(newEntity == null){
					newEntity =new Circle(entityIDCounter++ ,color ,width,style,
							x1 ,y1 , e.x ,e.y);}
					addEntity(newEntity , e.x ,e.y);
					break;
				case 4://rectangle
					if(newEntity == null){
						newEntity =new Rectangle(entityIDCounter++ ,color ,width,style,
								x1 ,y1 , e.x ,e.y);}
						addEntity(newEntity , e.x ,e.y);
					break;
				case 5://picture
					break;
				case 6://text
					if(newEntity ==null){
						if(choosedEntity!=null &&choosedEntity.getClass().getName().endsWith("model.drawPanel.entitys.Text")){
							choosedEntity.cancelChoosed();
						}
						newEntity = new Text(entityIDCounter++ ,color,x1 ,y1 ,e.x, e.y,null,textInput);
						newEntity.setChoosed();
						choosedEntity = newEntity;
					}
					addEntity(newEntity,e.x,e.y);
					
					break;
				case 7://fillcolor
					break;
				case 8://earaser;
					if(newEntity ==null){
						//背景色
						newEntity =new Pen(entityIDCounter++,new Color(Display.getCurrent(),255,255,255),width,x1,y1 ,e.x,e.y);
					}
					addEntity(newEntity, e.x, e.y);
					break;
				}
			}else if(moveStatus == 1){//进入元素内部且按住鼠标拖动表示移动
				//System.out.println("changeposition" + choosedID);
				changePosition(choosedEntity, e.x-x2, e.y-y2);
				
			}else if(moveStatus == 2){//进入选中元素缩放圈内部且按住鼠标移动表示缩放
				changeSize(choosedEntity ,zoomMsg,e.x-x2, e.y-y2);
				
			}
			x2 =e.x;
			y2 = e.y;
		}
		
		//2鼠标松开时
		//,遍历元素看鼠标是否进入某元素范围内或者之前被选中的元素的缩放圈中
		//，并改变相应的moveStatus值，没有则恢复空闲状态
		else{
			if(mouseStatus ==1){//画笔不关选中遍历
				return;
			}
			if(entityList.size()!=0){
				for(int i =entityList.size()-1 ; i >=0; i--){
					Entity en =entityList.get(i);
					String msg = en.isFocused(e.x, e.y);
					if(msg.equals("entity")){//进入元素
						moveStatus = 1;
						focusedEntity = en;
						return;
					}else if(msg.equals("noEnter")){
						continue;
					}else if (msg.startsWith("zoom")){
						moveStatus = 2;//进入选中元素的缩放圈
						zoomMsg = msg;//记录缩放类型信息
						return;
					}
				}
			}
			
			moveStatus = 0; 
			focusedEntity =null;
		}
		
		
	}
	@Override
	public void setColor(Color color) {
		 btnIsPressed();
		this.color = color;
		
	}
	@Override
	public void setTool(int tool) {
		 btnIsPressed();
		this.tool = tool;
		
	}
	@Override
	public void setLineStyle(int lineStyle) {
		 btnIsPressed();
		this.style = lineStyle;
		
	}
	@Override
	public void setFill(boolean hasFill) {
		 btnIsPressed();
		this.hasFill = hasFill;
		
	}
	@Override
	public synchronized void addPicture(String filePath) {
		 btnIsPressed();
		 if(authority ==false){
				return;
			}
		 if(filePath.equals("/")){
			 return;
		 }
		addEntity(new Picture(entityIDCounter++ ,20 , 20 ,new File(filePath) ) , -1 , -1);
		System.out.println("add picture is done");
		oneOrderIsDone();
		
	}
	@Override
	public void setWidth(int width) {
		this.width = width;
		
	}
	public void exite(){
		netModel.exite();
	}
	 private Entity getEntity(int id){
	    	for(Entity en :entityList){
	    		if(en.getId() == id){
	    			return en;
	    		}
	    	}
	    	return null;
	 }
	 private  void inite(IniteOrder order){
		 this.panelList = order.getPanelList();
		 int addPanelNum = panelList.size()-1;
		 for(int i = 0;i <addPanelNum;i++){
			 ui.addPanel();
		 }
		 ui.choosePanel(order.getPanelNum());
		 this.entityIDCounter = order.getEntityCounter();
		 this.entityList = panelList.get(order.getPanelNum()).getEntitList();
		 this.penList =panelList.get(order.getPanelNum()).getPenList();
		 this.temp_order = order.getTemp_order();
		 this.orderList = panelList.get(order.getPanelNum()).getOrderList();
		 this.redoList = panelList.get(order.getPanelNum()).getRedoList();
		 String ip = order.getChairmanIp();
		if(ip!=null){
			 ui.updateAuthoriy(ip);
			 System.out.println(ip);
		 }
		
	 }
	/* private boolean drawPanelInite(){
		 Thread inite = new Thread(new Runnable(){
			 public void run(){
				 try {
					Socket s = new Socket(DrawNetModel.serverIP,DrawNetModel.drawServerInitePort);
					ObjectInputStream ois = new ObjectInputStream(s.getInputStream());
					try {
						IniteOrder initeOrder = (IniteOrder)ois.readObject();
						inite(initeOrder);
					} catch (ClassNotFoundException e) {
						e.printStackTrace();
					}
					
				} catch (UnknownHostException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				};
			 }
		 });
		 return true;
	 }*/
	@Override
	public void keyPressed(KeyEvent arg0) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void keyReleased(KeyEvent e) {
		if(e.keyCode==8){
			
			if(choosedEntity ==null){
				return;
			}else{
				deleteEntity(choosedEntity);
			}
		}
		
	}
	 
}